<!DOCTYPE html>
<html>
<!--包含公共文件-->
  {include file='head'}

        <!--bootstrap分页-->
    <link rel="stylesheet" type="text/css" href="{$staticAdmin}dist/css/bootstrap.min.css">

<!--  这个标签内添加需要的内容  -->
<section class="rt_wrap content mCustomScrollbar">
 <div class="rt_content" name="div1">
      <div class="page_title">
       <h2 class="fl">会员列表</h2>
       <a href="{$webSite}admin/user/increase.html" class="fr top_rt_btn add_icon">添加新会员</a>
      </div>
      <section class="mtb">
       <select class="select" id="sel0">
        <option value="0">会员等级</option>
        {foreach $userLevel as $lev}
        <option value="{$lev['level_id']}">{$lev['level_name']}</option>
        {/foreach}

       </select>
       <input type="text" class="textbox textbox_225" name="put" placeholder="输入会员号/手机/电子邮件查询..."/>
       <input type="button" value="查询" class="group_btn" name="btn"/>

          <span class="tip"></span>
      </section>
      <table class="table" name="tb1">
       <tr >
        <th class="center">Id</th>
        <th class="center">会员头像</th>
        <th class="center">会员账号</th>
        <th class="center">手机号码</th>
        <th class="center">电子邮件</th>
        <th class="center">是否已激活</th>
        <th class="center">会员等级</th>
        <th class="center">账户余额</th>
        <!--<th>冻结资金</th>-->
        <th class="center">操作</th>
       </tr>

       <!--遍历用户信息-->
       {foreach $user as $u}
       <tr name="tr1">
        <td class="center">{$u['uid']}</td>
        <td class="center"><img src="{$webSite}{$u['pic']}" width="50" height="50"/></td>
        <td class="center">{$p = $u['phone'] == 1 ? '无' : $u['username'];}</td>
        <td class="center">{$p = $u['phone'] == 1 ? '无' : $u['phone'];}</td>
        <td class="center">{$p = $u['email'] == 1 ? '无' : $u['email'];}</td>

           {if $u['is_active'] == 1}
        <td class="center active"><a title="已验证" class="link_icon" name="{$u['uid']}" >&#89;</a></td>
           {else}
        <td class="center active"><a title="已验证" class="link_icon" name="{$u['uid']}" >X</a></td>
           {/if}

        {foreach $userLevel as $type}
          {if $type['level_id'] == $u['level_id']}
        <td class="center">{$type['level_name']}</td>
          {/if}
        {/foreach}
        <td class="center">
         <strong class="rmb_icon">{$u['grade']}</strong>
        </td>
       <!-- <td class="center">
         <strong class="rmb_icon">59.00</strong>
        </td>-->
        <td class="center">
         <a href="{$webSite}admin/user/edit?uid={$u['uid']}" title="编辑" class="link_icon edit">&#101;</a>
         <a href="{$webSite}admin/user/discard?uid={$u['uid']}&flag=discard" title="回收" class="link_icon update">&#100;</a>
        </td>
       </tr>
       {/foreach}
      </table>
     <!--分页-->
     <aside class="paging" name="page">
         {$userPage}
     </aside>

 </div>
</section>
</body>
</html>
<script>
    $(function () {

        //ajax查询,(获取单条数据,因为查询条件是唯一的,无法分页)
        $('[name=btn]').click(function () {

            var $oLev = $('#sel0').val();
           // console.log($oLev);
            var $oName = $('[name=put]').val().replace(/(^\s*)|(\s*$)/g, "");

            //邮箱规则
            var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

            //若为空
            if ($oName.length == 0) {

                $('.tip').html('查询条件不能为空').css('color','red');
                return false;

                //若为邮箱
            } else if(myreg.exec($oName)) {
alert(111);
                $.post('sel',{email:$oName,level:$oLev},success,'json');

                //若为手机
            } else if((/^1[34578]\d{9}$/.test($oName))) {

                //alert(222);
                $.post('sel',{phone:$oName,level:$oLev},success,'json');

            } else {
                //alert(3333);
                //按用户名处理
                $.post('sel',{username:$oName,level:$oLev},success,'json');
            }

        });

        //回调函数
        function success(data)
        {
            console.log(data);

            switch (data.status) {

                //ajax查询,存在
                case 1:
                    $('.tip').html('您查询的结果如下').css('color','green');
                    //console.log(data.res);

                    //获取数据
                    var $oRes = data.res;
                    var $oSite = data.webSite;
                    var $oLevel = data.level;
                    var $oActive = data.active;
                    var $arr = [];  //存放变量的数组
                    var $num = 0;
                    var flag = false;
                    var $tr = null;

                    if (!flag) {

                        //清空
                        //$('[name=tr1]').css('display','none');
                        $('[name=tr1]').remove();
                        $('[name=page]').css('display','none');

                        flag = true;

                        //批量创建表格行
                        var $total = 1;
                        for (var i = 0; i < $total; i++) {

                            //添加节点
                            $('[name=tb1]').append('<tr class="tr1" name="tr1"></tr>');
                            $num++;
                            //console.log($num);

                            //下标
                            for (var m = 0; m < 8; m++) {
                                $arr.push(($num-1)*8 + m);
                            }

                            //添加子节点td
                            $('.tr1').eq(i).append('<td class="center">'+ $oRes[$arr.shift()]['uid'] +'</td>');
                            $('.tr1').eq(i).append('<td class="center"><img src='+  $oSite + $oRes[$arr.shift()]['pic'] + ' width="50" height="50"/></td>');
                            $('.tr1').eq(i).append('<td class="center" >'+ $oRes[$arr.shift()]['username'] +'</td>');
                            $('.tr1').eq(i).append('<td class="center" >'+ $oRes[$arr.shift()]['phone'] + '</td>');
                            $('.tr1').eq(i).append('<td class="center" >'+ $oRes[$arr.shift()]['email']+'</td>');

                            //是否激活
                            $('.tr1').eq(i).append('<td class="center active"><a title="'+ $oActive[0] +'" class="link_icon" name="'+ $oActive[1] +'" >'+ $oActive[2] +'</a></td>');
                            //等级称号
                            $('.tr1').eq(i).append('<td class="center">'+ $oLevel['level_name'] +'</td>');

                            $('.tr1').eq(i).append('<td class="center"><strong class="rmb_icon">'+ $oRes[$arr.shift()]['grade'] +'</strong></td>&ndash;&gt;');
                            $('.tr1').eq(i).append('<td class="center"><a href="'+ $oSite +'admin/user/discard?uid='+ $oActive[1] +'" title="编辑" class="link_icon">&#101;</a><a href="'+ $oSite +'admin/user/discard?uid='+ $oActive[1] +'&flag=discard" title="回收" class="link_icon update">&#100;</a></td>');
                            //console.log($arr);
                        }
                    }
                    break;
                //不存在
                case 2:

                    $('.tip').html(data.tip).css('color','red');
                    //清空
                    $('[name=tr1]').remove();
                    $('[name=page]').css('display','none');
                    break;
            }
        }

    });


    //切换是否激活
    $(function () {

        //给a标签点击事件
        $('.active a').click(function () {

            //console.log($(this));
            //取值
            var $oAct = $(this)[0].innerText;   // Y或N
            var $uid = $(this).attr("name");
            //console.log($('.active a'));
            console.log($oAct);
            console.log($uid);

            if ($oAct == 'Y') {

                $oAct = 1;
            } else if ($oAct == 'X') {
                $oAct = 0;
            }
            //console.log($oAct);

            $.post('active',{uid:$uid,active:$oAct},success3,'json');

            //回调函数
            function success3(data)
            {
                //console.log(data);

                switch (data.state) {
                    //修改成功
                    case 1:
                        var $oActed = data.acted;
                        //console.log($oActed);
                        $('[name='+ $oActed[1] +']').replaceWith('<a title="'+ $oActed[0] +'" class="link_icon" name="'+ $oActed[1] +'">'+ $oActed[2] +'</a>');
                        alert(data.tip);
                        //重载当前页面
                        window.location.reload();
                        break;

                    //修改失败
                    case 2:
                        alert(data.tip);
                        break;
                }
            }

        });
    });

</script>
